﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.ComponentModel;
using System.Configuration;
namespace Classes
{
    public static class Uteis
    {

        public static void LimparCampos(Form local)
        {
            try
            {
                foreach (Control item in local.Controls)
                {
                    if (item.GetType() == typeof(GroupBox))
                    {
                        foreach (Control item2 in item.Controls)
                        {
                            if (item2.GetType() == typeof(TextBox))
                                (item2 as TextBox).Clear();
                        }

                    }
                    else if (item.GetType() == typeof(TextBox))
                    {
                        (item as TextBox).Clear();
                    }
                }
            }
            catch (Exception erro)
            {
                MessageBox.Show("Erro ao limpar campos.\nErro: " + erro.Message);
            }
        }

        //** **/




        public static string ToBase64(string Path)
        {
            Bitmap bm = new Bitmap(Path);//@"c:\download.png")
            string base64 = null;
            using (bm)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bm.Save(ms, GetImageFormat(Path));
                    base64 = Convert.ToBase64String(ms.ToArray());
                }
            }
            return base64;
        }
        // 
        //}


        private static ImageFormat GetImageFormat(string fileName)
        {
            string extension = Path.GetExtension(fileName);
            if (string.IsNullOrEmpty(extension))
                throw new ArgumentException(
                    string.Format("Unable to determine file extension for fileName: {0}", fileName));

            switch (extension.ToLower())
            {
                case @".bmp":
                    return ImageFormat.Bmp;

                case @".gif":
                    return ImageFormat.Gif;

                case @".ico":
                    return ImageFormat.Icon;

                case @".jpg":
                    return ImageFormat.Jpeg;
                case @".jpeg":
                    return ImageFormat.Jpeg;

                case @".png":
                    return ImageFormat.Png;

                case @".tif":
                case @".tiff":
                    return ImageFormat.Tiff;

                case @".wmf":
                    return ImageFormat.Wmf;

                default:
                    throw new NotImplementedException();
            }
        }

        public static string GetFileName(string filename) 
        { 
            return Path.GetFileName(filename);
        }

        public static string GetMIME(string fileName)
        {
            string extension = Path.GetExtension(fileName);
            if (string.IsNullOrEmpty(extension))
                throw new ArgumentException(
                    string.Format("Unable to determine file extension for fileName: {0}", fileName));

            switch (extension.ToLower())
            {
                case @".bmp":
                    return "image/bmp";

                case @".gif":
                    return "image/gif";

               // case @".ico":
               //     return "image/png";

                case @".jpg":
                    return "image/jpeg";
                case @".jpeg":
                    return "image/jpeg";

                case @".png":
                    return "image/png";

                case @".tif":
                case @".tiff":
                    return "image/tiff";

              //  case @".wmf":
              //      return "image/tiff";

                default:
                    throw new NotImplementedException();
            }
        }



    }
}
